package com.cqjtu.store.order.controller;

import com.cqjtu.store.common.JwtUtils;
import com.cqjtu.store.common.PageDTO;
import com.cqjtu.store.common.PageVO;
import com.cqjtu.store.common.R;
import com.cqjtu.store.order.pojo.dto.OrderListDTO;
import com.cqjtu.store.order.pojo.dto.OrderSearchDTO;
import com.cqjtu.store.order.pojo.vo.OrderVO;
import com.cqjtu.store.order.service.OrderService;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ProjectName: cqjtu-store
 * @Titile: OrderController
 * @Author: Lucky
 * @Description: 订单控制类
 */
@RestController
@RequestMapping("api/order")
@Api(tags = "订单模块")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 获取用户身份
     * @param request
     * @return 登陆之后的用户
     */
    public UserLoginVO parseToken(HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        return userLoginVO;
    }
    @GetMapping("list")
    @ApiOperation("展示订单列表")
    public R<List<OrderVO>> getOrderList(HttpServletRequest request,
                                         @RequestParam(value = "status", required = false) Integer status){
        UserLoginVO userLoginVO = this.parseToken(request);
        List<OrderVO> orderVOList =  orderService.getList(userLoginVO, status);
        return R.ok(orderVOList);
    }

    @PutMapping("list")
    @ApiOperation("展示订单列表,(结算后使用)")
    public R<List<OrderVO>> getOrderList(@RequestBody OrderListDTO orderListDTO, HttpServletRequest request){
        UserLoginVO userLoginVO = this.parseToken(request);
        List<OrderVO> orderVOList =  orderService.getList(userLoginVO, orderListDTO);
        return R.ok(orderVOList);
    }

//    @PutMapping("list")
//    @ApiOperation("展示订单列表")
//    public R<List<OrderVO>> getOrderList(@RequestBody OrderListDTO orderListDTO,HttpServletRequest request){
//        UserLoginVO userLoginVO = this.parseToken(request);
//        List<OrderVO> orderVOList =  orderService.getList(userLoginVO, orderListDTO);
//        return R.ok(orderVOList);
//    }

    @PutMapping("listAll")
    @ApiOperation("展示订单列表")
    public R<PageVO<List<OrderVO>>> getOrderList(@RequestBody PageDTO pageDTO){
        System.out.println(pageDTO);
        PageVO<List<OrderVO>> orderVOList =  orderService.getAllList(pageDTO);
        return R.ok(orderVOList);
    }

    @PutMapping("receive")
    @ApiOperation("确认收货")
    public R<Void> receive(HttpServletRequest request,
                           @RequestParam("oid") Integer oid){
        UserLoginVO userLoginVO = this.parseToken(request);
        this.orderService.receive(userLoginVO, oid);
        return R.ok();
    }
    @GetMapping("getTotal")
    public R<Integer> getTotal(){
         Integer total = orderService.getTotal();
        return R.ok(total);
    }
    @DeleteMapping("delete/{id}")
    public R<Void> delete(@PathVariable Integer id){
        orderService.delete(id);
        return R.ok();
    }
    @PutMapping("search")
    public R<PageVO<List<OrderVO>>> search(@RequestBody OrderSearchDTO orderSearchDTO){
        PageVO<List<OrderVO>> orderVOList =  orderService.getSomeList(orderSearchDTO);
        return R.ok(orderVOList);
    }
}
